package com.sifang.base.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class SignUtil {
	
	/**
	 * 签名算法
	 * @param o 参与签名的拼接报文数据对象
	 * @param key
	 * @return 签名
	 * @throws IllegalAccessException
	 * @throws UnsupportedEncodingException
	 */
	public static String getSignReq(String str, String key) {
		String[] levelOne = str.split("&");
		ArrayList<String> list = new ArrayList<String>();
		for(int i=0;i<levelOne.length;i++) {
			String[] levelTwo = levelOne[i].split("=");
			if(null != levelTwo && levelTwo.length==2) {
				if(!levelTwo[0].equals("sign")) {
					list.add(levelOne[i]+"&");
				}
				
			}
		}
		int size = list.size();
		String[] arrayToSort = list.toArray(new String[size]);
		Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			sb.append(arrayToSort[i]);
		}
		String result = sb.toString();
		result += "key=" + key;
		result = MD5.MD5Encode(result).toUpperCase();//MD5
		return result;
	}
	
	/**
	 * 获取签名算法后的拼接报文
	 * @param o 参与签名的json数据对象
	 * @param key
	 * @return 签名
	 * @throws IllegalAccessException
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("rawtypes")
	public static String getSignReq(Object o, String key)
			throws IllegalAccessException, UnsupportedEncodingException {
		ArrayList<String> list = new ArrayList<String>();
		JSONObject jobj = JSONObject.fromObject(o);
		//获取json中的key
		Iterator it = jobj.keys();
		while (it.hasNext()) { 
			String jsonkey = it.next().toString();
			if (!"sign".equals(jsonkey) &&  jobj.getString(jsonkey) != null && !"".equals(jobj.getString(jsonkey))) {
				if(!(jobj.get(jsonkey) instanceof JSONArray)) {
					list.add(jsonkey + "=" + jobj.getString(jsonkey) + "&");
				}
			}
		}
		int size = list.size();
		String[] arrayToSort = list.toArray(new String[size]);
		StringBuilder oriSb = new StringBuilder();
		for(int i=0;i<size;i++) {
			oriSb.append(arrayToSort[i]);
		}
		Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < size; i++) {
			sb.append(arrayToSort[i]);
		}
		String result = sb.toString();
		result += "key=" + key;
	    System.out.println(result.getBytes("UTF-8"));
	    System.out.println("result:"+result);
        result = MD5.MD5Encode(result).toUpperCase();//MD5
        oriSb.append("key=");
        oriSb.append(result);
		return oriSb.toString();
	}
	
	
	/**
	 * 根据拼接串得到Map
	 */
	public static Map<String,Object> getMapFromStr(String str){
		
		if(null == str || str.length()<10) {
			return null;
		}
		
		Map<String,Object> keyValues = new HashMap<>();
		
		String[] levelOne = str.split("&");
		if(null != levelOne && levelOne.length>0) {
			for(int i=0;i<levelOne.length;i++) {
				String[] levelSecond = levelOne[i].split("=");
				if(null != levelSecond && levelSecond.length>0) {
					if(levelSecond.length==1) {
						keyValues.put(levelSecond[0], "");
					}else {
						keyValues.put(levelSecond[0], levelSecond[1]);
					}
					
				}
			}
		}else {
			return null;
		}
		return keyValues;
	}

}
